﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QLNhaSach.BUS.DTO;
using QLNhaSach.BUS;

namespace QLNhaSach.GUI.NhanVienQuanLy
{
    public partial class ThemQuyDinh : Form
    {
        QuyDinhDTO qd = new QuyDinhDTO();

        public ThemQuyDinh()
        {
            InitializeComponent();
        }

        private void ClearForm()
        {
            txtMaQD.Clear();
            txtMoTaQD.Clear();
            txtValue.Clear();
            rb1.Select();
            txtMaQD.Focus();
        }

        private void ShowListView(DataTable dt)
        {
            lvDSQD.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                ListViewItem item = new ListViewItem();
                item.Text = dr["MaQD"].ToString();
                item.SubItems.Add(dr["MoTa"].ToString());
                item.SubItems.Add(dr["GiaTri"].ToString());
                item.SubItems.Add(dr["TinhTrang"].ToString());
                lvDSQD.Items.Add(item);
            }
        }

        private void ThemListView(QuyDinhDTO qd)
        {
            ListViewItem lvi = new ListViewItem();
            lvi.Text = txtMaQD.Text;
            lvi.SubItems.Add(txtMoTaQD.Text);
            lvi.SubItems.Add(txtValue.Text);
            if (rb1.Checked)
                lvi.SubItems.Add("1");
            else
                lvi.SubItems.Add("0");
            lvDSQD.Items.Add(lvi);
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            ClearForm();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if ((txtMaQD.Text != "") && (txtValue.Text != "") && ((rb1.Checked || rb2.Checked) == true))
            {
                long temp;
                if (long.TryParse(txtValue.Text, out temp) == false)
                {
                    MessageBox.Show("Giá trị là kiểu số");
                    txtValue.Text = "";
                    txtValue.Focus();
                    return;
                }
                qd.MaQD = txtMaQD.Text;
                qd.GiaTri = Convert.ToInt32(txtValue.Text);
                qd.MoTa = txtMoTaQD.Text;
                if (rb1.Checked)
                    qd.TinhTrang = 1;
                else
                    qd.TinhTrang = 0;
                if (NhanVienQuanLyBUS.TaoQD(qd) == NhanVienQuanLyBUSResult.DAOERROR)
                {
                    MessageBox.Show("Loi CSDL, Xin Thu Lai Lan Sau ! ^_^ !");
                    ThemListView(qd);
                }
                else
                {
                    MessageBox.Show("Tao Quy Dinh Moi Thanh Cong ! ^_^ !");
                    ThemListView(qd);
                }
            }
            else
            {
                MessageBox.Show("Thong Tin Khong Day Du, Moi Kiem Tra lai !!");
            }
            ClearForm();
        }

        private void btnTuyChon_Click(object sender, EventArgs e)
        {
            DataTable dt = NhanVienQuanLyBUS.DSQD();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (txtMaQD.Text == dt.Rows[i][1].ToString())
                {
                    MessageBox.Show("Ma Quy Dinh Da Ton Tai, Moi Ban Nhap Moi !!!");
                    ClearForm();
                }
                else
                    MessageBox.Show("Ma Quy Dinh Hop Le, Xin Tiep Tuc !!!");
            }
        }

        private void ThemQuyDinh_Load(object sender, EventArgs e)
        {
            DataTable dt = NhanVienQuanLyBUS.DSQD();
            ShowListView(dt);            
        }

       
    }
}